<template>
  <div class="reportinspect-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>
        <el-button
          :disabled="!currentRow"
          :icon="Edit"
          plain
          type="warning"
          @click="check"
        >
          检验
        </el-button>
        <el-button
          :disabled="!currentRef"
          :icon="Edit"
          plain
          type="warning"
          @click="update"
        >
          关联来源
        </el-button>
        <el-button
          :disabled="!currentRef"
          :icon="Delete"
          plain
          type="danger"
          @click="deleteEquip"
        >
          删除
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :cell-style="{ padding: '6px 6px' }"
      :data="inspects"
      highlight-current-row
      @current-change="handleCurrentChange"
      style="height: calc(100vh - 186px)"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="报检单号" prop="inspectCode" width="180" />
      <el-table-column label="报检类型" width="200">
        <template #default="scope">
          <el-row>
            <span style="font-size: 16px; font-weight: 700; color: #000">
              {{ scope.row.inspectTypeName }}
            </span>
          </el-row>
          <el-row>
            <span>
              {{ scope.row.sourcePk }}
            </span>
          </el-row>
        </template>
      </el-table-column>

      <el-table-column label="存货编码" prop="invcode" />
      <el-table-column label="名称型号" prop="invcode">
        <template #default="scope">
          <el-row>
            <span style="font-size: 16px; font-weight: 700; color: #000">
              {{ scope.row.invname }}
            </span>
          </el-row>
          <el-row>
            <span>
              {{ scope.row.invspec }}
            </span>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="报检数" width="150">
        <template #default="scope">
          <span style="font-size: 16px; font-weight: 600; color: #5314bd">
            {{ scope.row.qty }}&nbsp;{{ scope.row.measName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="检验数" width="150">
        <template #default="scope">
          <span style="font-size: 16px; font-weight: 600; color: #5314bd">
            {{ scope.row.checkQty }}&nbsp;{{ scope.row.measName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="检验完毕" width="120">
        <template #default="scope">
          <el-icon v-if="scope.row.state == 1" color="#30DF80" size="20">
            <Select />
          </el-icon>
          <el-icon v-else color="#DF3730" size="20">
            <SemiSelect />
          </el-icon>
        </template>
      </el-table-column>
      <el-table-column label="创建信息" prop="invcode" width="220">
        <template #default="scope">
          <el-row>
            <span>
              {{ scope.row.createUser }}
            </span>
          </el-row>
          <el-row>
            <span>
              {{ scope.row.createTime }}
            </span>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <checkDialog ref="checkDialogRef" @success-callback="checkCallback" />
    <queryInspect ref="queryInspectRef" @success-callback="queryCallback" />
  </div>
</template>

<script setup>
  import { Search, Delete, Select, SemiSelect } from '@element-plus/icons-vue'
  import { localStorageConfig } from '@/config/localstorage.config'
  import { queryReportInspectPageApi } from '@/api/qm/index'
  import checkDialog from './modules/checkDialog.vue'
  import queryInspect from './modules/queryInspect.vue'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const queryForm = ref({ pkCorp: pkCorp, state: 0 })
  const currentRow = ref()
  const checkDialogRef = ref()

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 20,
    total: 0,
  })

  const inspects = ref()
  const queryInspectRef = ref()

  const queryReportInspectPage = async () => {
    const load = $baseLoading()
    try {
      let params = {
        ...queryForm.value,
        ...paginationParams,
      }
      const { code, msg, data } = await queryReportInspectPageApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        inspects.value = data.list
        paginationParams.total = data.total
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const handleCurrentChange = async (row) => (currentRow.value = row)

  const openQueryDialog = () => queryInspectRef.value.openDialog()

  const queryCallback = (value) => {
    queryForm.value = value
    queryForm.value.ksDate = value.queryDate[0]
    queryForm.value.jsDate = value.queryDate[1]
    queryReportInspectPage()
  }

  const check = () => {
    if (currentRow.value.state != 0) {
      $baseMessage('该报检单已经检验完毕', 'warning')
      return
    }
    checkDialogRef.value.openDialog(currentRow.value)
  }

  const checkCallback = () => queryReportInspectPage()

  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
  }
  watch(paginationParams, () => queryReportInspectPage())

  onMounted(() => queryReportInspectPage())
</script>
